package com.zbec.mdrs.web;

import com.zbec.framework.core.constant.Constants;
import com.zbec.framework.core.entity.SystemDictionary;
import com.zbec.framework.core.service.SystemDictionaryTypeService;
import com.zbec.framework.security.service.SecurityService;
import com.zbec.mdrs.service.SysPasswordService;
import com.zbec.mdrs.vo.ConfigPasswordVo;
import org.json.simple.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by fxl on 2015/12/1.
 */
@Controller
public class SysPasswordController extends BaseController {
    @Autowired
    private SystemDictionaryTypeService systemDictionaryTypeService;

    @Autowired
    private SysPasswordService sysPasswordService;

    @Autowired
    private SecurityService securityService;

    @RequestMapping(value = "/system/systemPassword")
    public ModelAndView toConfig(HttpServletRequest request) {
        ModelAndView view=new ModelAndView("system/configPassword");
        ConfigPasswordVo vo=new ConfigPasswordVo();

        SystemDictionary defaultPassCode = this.systemDictionaryTypeService.findSystemDictionarybyTypeCode(Constants.CODE_TYPE_DEFAULT_PASSWORD);
        if(defaultPassCode!=null){
            vo.setDefaultPassword(defaultPassCode.getCodeValue());
            vo.setDefaultPasswordId(defaultPassCode.getId());
        }

        SystemDictionary expireDaysCode = this.systemDictionaryTypeService.findSystemDictionarybyTypeCode(Constants.CODE_TYPE_ACCOUNT_EXPIRE_DAYS);
        if(expireDaysCode!=null) {
            vo.setExpireDays(expireDaysCode.getCodeValue());
            vo.setExpireDaysId(expireDaysCode.getId());
        }

        SystemDictionary loginCheckCode = this.systemDictionaryTypeService.findSystemDictionarybyTypeCode(Constants.CODE_TYPE_INVALID_LOGIN_CHECK);
        if(loginCheckCode!=null) {
            vo.setLoginCheck(loginCheckCode.getCodeValue());
            vo.setLoginCheckId(loginCheckCode.getId());
        }

        SystemDictionary expireCheckCode = this.systemDictionaryTypeService.findSystemDictionarybyTypeCode(Constants.CODE_TYPE_ACCOUNT_EXPIRE_CHECK);
        if(expireCheckCode!=null) {
            vo.setExpireCheck(expireCheckCode.getCodeValue());
            vo.setExpireCheckId(expireCheckCode.getId());
        }

        SystemDictionary samePasswordCode = this.systemDictionaryTypeService.findSystemDictionarybyTypeCode(Constants.CODE_TYPE_USE_SAME_PASSWORD);
        if(samePasswordCode!=null) {
            vo.setSamePassword(samePasswordCode.getCodeValue());
            vo.setSamePasswordId(samePasswordCode.getId());
        }

        SystemDictionary modifyPassword = this.systemDictionaryTypeService.findSystemDictionarybyTypeCode(Constants.CODE_TYPE_USER_MODIFY_PASSWORD);
        if(modifyPassword!=null) {
            vo.setModifyPassword(modifyPassword.getCodeValue());
            vo.setModifyPasswordId(modifyPassword.getId());
        }
        view.addObject("configPasswordVo",vo);
        return view;
    }

    @RequestMapping(value = "/system/configPassword")
    @ResponseBody
    public String configPassword(HttpServletRequest request,ConfigPasswordVo vo) {
        sysPasswordService.configPassword(vo);
        JSONObject object=new JSONObject();
        object.put("result","true");
        return object.toJSONString();
    }

    @RequestMapping(value = "/system/systemPassword/toEditPassword")
    public ModelAndView toEditPassword(HttpServletRequest request) {
        ModelAndView view=new ModelAndView("system/editPassword");
        view.addObject("userId",securityService.getCurrentUserVO().getUserId());
        return view;
    }

    @RequestMapping(value = "/system/isPasswordWillExpire")
    @ResponseBody
    public String isPasswordWillExpire(){
        Long day=sysPasswordService.isPasswordWillExpire(securityService.getCurrentUserId());
        JSONObject object=new JSONObject();
        if(day!=0L){
            String tip="密码将在"+day.toString()+"天内过期";
            object.put("tip",tip);
            object.put("result","true");
        }else{
            object.put("result","false");
        }
        return object.toJSONString();
    }

}
